package com.hghivln.erpwarehouse.system.controller;


import com.hghivln.erpwarehouse.system.common.lang.*;
import com.hghivln.erpwarehouse.system.entity.User;
import com.hghivln.erpwarehouse.system.service.UserService;
import com.hghivln.erpwarehouse.system.utils.MD5Utils;
import com.hghivln.erpwarehouse.system.vo.UserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hghivln
 * @since 2021-06-21
 */
@RestController
@RequestMapping("api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询用户所有信息
     */
    @GetMapping("loadAllUser")
    public Object loadAllMenu(UserVo userVo) {
        return userService.queryAllUser(userVo);
    }

    /**
     * 用户添加
     */
    @PostMapping("addUser")
    @RequiresPermissions("user:add")
    public Result addMenu(User user) {
        try {
            user.setSalt(MD5Utils.createUUID());
            user.setPwd(MD5Utils.md5(Constant.USER_DEFALT_PWD, user.getSalt(), Constant.HASHITERATIONS));
            user.setImgpath("");
            userService.addUser(user);
            return Result.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    /**
     * 用户修改
     */
    @PostMapping("updateUser")
    @RequiresPermissions("user:update")
    public Result updateMenuBtn(User user) {
        try {
            userService.updateUser(user);
            return Result.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    /**
     * 用户删除
     */
    @PostMapping("deleteUser")
    @RequiresPermissions("user:delete")
    public Result deleteMenu(Integer id) {
        try {
            userService.removeById(id);
            return Result.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.DELETE_ERROR;
        }
    }

    /**
     * 查询最大排序码
     */
    @GetMapping("queryUserMaxOrderNum")
    public Object queryDeptMaxOrderNum() {
        return userService.queryUserMaxOrderNum();
    }

    /**
     * 根据用户ID查询当前角色已拥有的角色ID集合
     */
    @GetMapping("queryRoleIdsByUid")
    public Object queryRoleIdsByUid(Integer uid) {
        return userService.queryRoleIdsByUid(uid);
    }

    /**
     * 保存角色和用户之间的关系
     */
    @PostMapping("saveUserRole")
    public Result saveUserRole(Integer uid, Integer[] rids) {
        try {
            userService.saveUserRole(uid, rids);
            return Result.DISPATCH_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.DISPATCH_ERROR;
        }
    }

    /**
     * 重置密码
     */
    @PostMapping("resetUserPwd")
    @RequiresPermissions("user:reset")
    public Result resetUserPwd(Integer id) {
        try {
            userService.resetUserPwd(id);
            return Result.RESET_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.RESET_ERROE;
        }
    }

    /**
     * 修改密码
     */
    @PostMapping("changePwd")
    public Object changePassword(String oldPassword, String newPassword, String againPassword, Integer id) {

        return userService.changePassword(oldPassword, newPassword, againPassword, id);

    }

    /**
     * 查看当前登录用户
     */
    @GetMapping("getCurrentUser")
    public Object getCurrentUser() {
        ActiverUser activerUser = (ActiverUser) SecurityUtils.getSubject().getPrincipal();
        System.out.println(activerUser);
        return new DataGridlView(activerUser.getUser());
    }


}
